/*
 * @Author: Aro
 * @Date: 2024-01-29 09:54:46
 */
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_baseui/routers/fluros.dart';

class FluroNaviagtor {
  static void push(
    BuildContext context,
    String path, {
    bool replace = false,
    bool clearStack = false,
    Object? arguments,
  }) {
    unfocus();
    Fluros.router.navigateTo(
      context,
      path,
      replace: replace,
      clearStack: clearStack,
      transition: TransitionType.native,
      routeSettings: RouteSettings(arguments: arguments),
    );
  }

  static void pushResult(
    BuildContext context,
    String path,
    void Function(Object) function, {
    bool replace = false,
    bool clearStack = false,
    Object? arguments,
  }) {
    unfocus();
    Fluros.router
        .navigateTo(
      context,
      path,
      replace: replace,
      clearStack: clearStack,
      transition: TransitionType.native,
      routeSettings: RouteSettings(arguments: arguments),
    )
        .then((Object? result) {
      if (result == null) return;
      function(result);
    }).catchError((dynamic error) {
      debugPrint('$error');
    });
  }

  static void goBack(BuildContext context) {
    unfocus();
    Navigator.pop(context);
  }

  static void goBackWithParams(BuildContext context, Object result) {
    unfocus();
    Navigator.pop(context, result);
  }

  // TODO 完善webview
  static void goWebviewPage(BuildContext context, String title, String url) {}

  static void unfocus() {
    FocusManager.instance.primaryFocus?.unfocus();
  }
}
